﻿namespace AutoLite.Revit.Extensions;

internal static class TypeExtensions
{
    internal static bool IsGenericType(this Type type, Type genericType)
    {
        if (type.IsGenericType)
        {
            return type.GetGenericTypeDefinition() == genericType;
        }

        return false;
    }

    internal static Type? GetGenericInterface(this Type type, Type genericInterface)
    {
        if (IsGenericType(type, genericInterface))
        {
            return type;
        }

        Type[] interfaces = type.GetInterfaces();
        for (int num = interfaces.Length - 1; num >= 0; num--)
        {
            Type type2 = interfaces[num];
            if (IsGenericType(type2, genericInterface))
            {
                return type2;
            }
        }

        return null;
    }
}
